﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimeAlert
{
    public partial class ExamForm : Form
    {
        public ExamForm()
        {
            InitializeComponent();
            // 为 Date 列创建 DateTimePicker
            // DataGridViewCalendarColumn calendarColumn = new DataGridViewCalendarColumn();
            //  dgv.Columns["Date"].CellTemplate = calendarColumn.CellTemplate;

            // 为列设置编辑器
            this.dataGridView1.CellMouseDoubleClick += (sender, e) =>
            {
                if (e.ColumnIndex == dataGridView1.Columns["StartTime"].Index)
                {
                    var dateTimePicker = new DateTimePicker();
                    dateTimePicker.Format = DateTimePickerFormat.Custom;
                    dateTimePicker.CustomFormat = "yyyy-MM-dd HH:mm";
                    dateTimePicker.ShowUpDown = true;
                    //dateTimePicker.Font = new Font(new FontFamily("Microsoft Sans Serif"), 12.5f, FontStyle.Regular);
                    dataGridView1.Controls.Add(dateTimePicker);
                    dateTimePicker.Location = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                    dateTimePicker.Size = new Size(150, 50);
                    string str = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
                    if (string.IsNullOrEmpty(str))
                    {
                        dateTimePicker.Value = DateTime.Now;
                    }
                    else
                    {
                        dateTimePicker.Value = DateTime.Parse(str);
                    }

                    dateTimePicker.LostFocus += (s, ev) =>
                    {
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dateTimePicker.Value.ToString("yyyy-MM-dd HH:mm");
                        dataGridView1.Controls.Remove(dateTimePicker);
                    };
                }
            };

            // 添加到窗体
            this.Controls.Add(dataGridView1);

            searchExam_Click(null, null);
        }

        private void searchExam_Click(object sender, EventArgs e)
        {
            this.examSet.Clear();
            var exams = Business.GetExamFullList();

            // 2. List 转 DataTable
            DataTable dt = Helper.ExamToDataTable<ExamDto>(exams);

            // 3. DataTable 转 DataSet（添加到 DataSet 中）
            this.examSet.Tables.Clear();


            this.examSet.Tables.Add(dt); // 将 DataTable 加入 DataSet
            this.examSet.Tables[0].TableName = "ExamTable"; // 可选：给表命名

            // 4. 绑定到 DataGridView
            dataGridView1.DataSource = this.examSet.Tables["ExamTable"]; // 直接绑定 DataSet 中的表

            for (int i = 0; i < exams.Count; i++)
            {
                var exam = exams[i];
                Button[] btn = new Button[2];
                btn[0] = new Button();
                btn[0].Text = "编辑";
                btn[1] = new Button();
                btn[1].Text = "删除";
                this.dataGridView1.Controls.Add(btn[0]);
                this.dataGridView1.Controls.Add(btn[1]);
                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(4, i, false);
                btn[0].Size = btn[1].Size = new Size((rect.Width - 10) / 2, rect.Height - 4);
                btn[0].Location = new Point(rect.Left + 2, rect.Top + 2);
                btn[0].Name = "btn_edit_" + exam.Id + "_" + i;
                btn[1].Name = "btn_delete_" + exam.Id + "_" + i;
                btn[1].Location = new Point(rect.Left + btn[0].Width + 4, rect.Top + 2);
                btn[0].Click += (ss, ee) => { CustomBtn_Click(ss, ee, "edit", exam.Id); };
                btn[1].Click += (ss, ee) => { CustomBtn_Click(ss, ee, "delete", exam.Id); };
            }


        }

        void CustomBtn_Click(object sender, EventArgs e, string type, string id)
        {

            int row = int.Parse((sender as Button).Name.Split('_')[3]);
            this.dataGridView1.Rows[row].ReadOnly = false;

        }

        private void Save_Click(object sender, EventArgs e)
        {
            Helper.SaveExamList<Exam>(this.dataGridView1.DataSource);
        }
    }
}
